//
//  ZJWebViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/23.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJWebViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "YBNormalSelectDeptViewController.h"
@interface ZJWebViewController ()<UIWebViewDelegate , LPChooseYearAndMonthAndDayAndHourAndMinuteDelegate , YBNormalSelectDeptViewControllerDelegate>
/** webView */
@property (nonatomic, strong) UIWebView *webView;

@property (nonatomic , strong) LPChooseYearAndMonthAndDayAndHourAndMinuteView * pickTime;

@property (nonatomic , strong) NSString * currentTimeTypeName;
@end

@implementation ZJWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentTimeTypeName = @"";
    self.titlelabel.text = @"加载中...";
    [self.view addSubview:self.webView];
}



- (void)action_leftButton:(UIButton *)sender{
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    }else{
        if (self.isModel) {
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (void)ChooseYearAndMonthAndDayAndHourAndMinuteWithTitle:(NSString * )string{
    //选择年月日返回代理方法
    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    [context evaluateScript:[NSString stringWithFormat:@"window.%@({type:'%@',value:'%@'})" , @"getAppTime" , self.currentTimeTypeName , string]];
    
}

- (void)option_selectTimeWithCurrentTimeString:(NSString *)currentTimeString{
    [self.pickTime showCurrentTime];
}


-(void)ybNormalSelectDeptViewControllerDidSelectUnitWithName:(NSString *)name unitId:(NSString *)unitId
{
    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    [context evaluateScript:[NSString stringWithFormat:@"window.%@({deptName:'%@',deptId:'%@'})" , @"setUnit" , name , unitId]];
}



#pragma mark - UIWebView 协议方法
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
//    context[@"setTitle"] = ^{
////        //获取当前页面的标题
////        NSString * titleString = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
////        if (titleString.length == 0) {
////            self.titlelabel.text = self.titleString;
////        }
////        else{
////            self.titlelabel.text = titleString;
////        }
//        NSArray * tempArray = [JSContext currentArguments];
//        self.titlelabel.text = [tempArray[0] toString];
//
//    };
    
    self.titlelabel.text = self.titleString;
    
    [YJProgressHUD hide];
    if ([self.titleString isEqualToString:@"分享点"]) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //js代码
        NSString *jsString = @"var fxd =document.getElementById('fxd'); fxd.onclick = function(){go();} ";
        
        __weak typeof(self) weakSelf = self;
        [webView stringByEvaluatingJavaScriptFromString:jsString];
        //其中 openImage 是JS的函数名，得到的 arg数组 里面为JS的 passValue 函数的参数，即 JS要传给OC的参数。
//        JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
        context[@"go"] = ^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [[NSNotificationCenter defaultCenter] postNotificationName:SHAREMORE object:nil];

            });
        };
            
    });
        
    }
    if ([self.titleString isEqualToString:@"填写回执单"] || [self.titleString isEqualToString:@"报名"]) {
        __weak typeof(self) weakSelf = self;
        context[@"iosGetDept"] = ^{
            YBNormalSelectDeptViewController * vc = [[YBNormalSelectDeptViewController alloc]init];
            vc.delegate = weakSelf;
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.navigationController pushViewController:vc animated:YES];
            });
        };
        context[@"iosGetTime"] = ^{
            NSArray * args = [JSContext currentArguments];
            JSValue * nameValue = args[0];
            weakSelf.currentTimeTypeName = [ToolClass returnNoNullStringWithString:[nameValue toString]];
            JSValue * timeValue = args[1];
            NSString * timeString = [timeValue toString];
            [weakSelf option_selectTimeWithCurrentTimeString:[ToolClass returnNoNullStringWithString:timeString]];
        };
        context[@"iosBack"] = ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
        };
    }
    
}

- (void)webViewDidStartLoad:(UIWebView *)webView{

    //    [YJProgressHUD hide];
}




#pragma mark - Setter


- (void)setUrlString:(NSString *)urlString{
    _urlString = urlString;
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
     [YJProgressHUD showMessage:@"加载中..." duration:10 view:self.view];
}




#pragma mark - Getters

- (UIWebView *)webView{
    if (_webView == nil){
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - 64 )];
        _webView.delegate = self;
        _webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber | UIDataDetectorTypeLink | UIDataDetectorTypeShipmentTrackingNumber | UIDataDetectorTypeFlightNumber | UIDataDetectorTypeLookupSuggestion;
        _webView.backgroundColor = [UIColor clearColor];
        [_webView setOpaque:YES];
    }
    return _webView;
}


- (LPChooseYearAndMonthAndDayAndHourAndMinuteView *)pickTime{
    if (!_pickTime) {
        _pickTime  = [[LPChooseYearAndMonthAndDayAndHourAndMinuteView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
        _pickTime.delegate = self;
    }
    return _pickTime;
}



@end
